import { ref, computed } from 'vue'
import type { Tutorial, TutorialStep, TutorialProgress } from '../types/tutorial'

export function useTutorialSystem() {
  const tutorials = ref<Tutorial[]>([
    {
      id: 'basic-moves',
      name: '基本走法',
      difficulty: 'beginner',
      category: 'basic',
      steps: [
        {
          id: 1,
          title: '兵的移动',
          description: '兵只能向前移动一格。第一次移动时可以选择移动一格或两格。',
          board: [
            Array(8).fill(null),
            Array(8).fill(null),
            Array(8).fill(null),
            Array(8).fill(null),
            Array(8).fill(null),
            Array(8).fill(null),
            ['white-pawn', null, null, null, null, null, null, null],
            [null, null, null, null, null, null, null, null]
          ],
          highlightSquares: [{ row: 6, col: 0 }],
          validMoves: [
            { row: 5, col: 0 },
            { row: 4, col: 0 }
          ],
          expectedMove: {
            from: { row: 6, col: 0 },
            to: { row: 4, col: 0 }
          }
        },
        // ... 更多步骤
      ],
      completionReward: 100
    },
    // ... 更多教程
  ])

  const currentTutorial = ref<Tutorial | null>(null)
  const currentStep = ref<number>(0)
  const progress = ref<TutorialProgress[]>([])

  const availableTutorials = computed(() => {
    return tutorials.value.filter(tutorial => {
      if (!tutorial.prerequisites) return true
      return tutorial.prerequisites.every(prereqId => 
        progress.value.some(p => p.tutorialId === prereqId && p.completed)
      )
    })
  })

  const startTutorial = (tutorialId: string) => {
    const tutorial = tutorials.value.find(t => t.id === tutorialId)
    if (!tutorial) return false

    currentTutorial.value = tutorial
    currentStep.value = 0
    return true
  }

  const checkMove = (
    fromRow: number,
    fromCol: number,
    toRow: number,
    toCol: number
  ): boolean => {
    if (!currentTutorial.value) return false

    const step = currentTutorial.value.steps[currentStep.value]
    const expected = step.expectedMove

    return (
      fromRow === expected?.from.row &&
      fromCol === expected?.from.col &&
      toRow === expected?.to.row &&
      toCol === expected?.to.col
    )
  }

  const nextStep = () => {
    if (!currentTutorial.value) return false

    if (currentStep.value < currentTutorial.value.steps.length - 1) {
      currentStep.value++
      return true
    }

    // 完成教程
    const tutorialProgress = progress.value.find(
      p => p.tutorialId === currentTutorial.value!.id
    )

    if (tutorialProgress) {
      tutorialProgress.completed = true
      tutorialProgress.completedAt = new Date()
    } else {
      progress.value.push({
        tutorialId: currentTutorial.value.id,
        currentStep: currentTutorial.value.steps.length,
        completed: true,
        mistakes: 0,
        timeSpent: 0,
        completedAt: new Date()
      })
    }

    return false
  }

  const getCurrentStep = (): TutorialStep | null => {
    if (!currentTutorial.value) return null
    return currentTutorial.value.steps[currentStep.value]
  }

  const getTutorialProgress = (tutorialId: string): TutorialProgress | null => {
    return progress.value.find(p => p.tutorialId === tutorialId) || null
  }

  return {
    tutorials,
    availableTutorials,
    currentTutorial,
    currentStep,
    progress,
    startTutorial,
    checkMove,
    nextStep,
    getCurrentStep,
    getTutorialProgress
  }
} 